from tornado.web import RequestHandler
import logging
import traceback  

from core.decorators.open_handler import open_handler
from core.decorators.open_api.index import BaseRequestHandler, openApiFactory


@open_handler(path="/api/(.*)", index=1)
class openApiHandlerService(BaseRequestHandler):

    # 在这里复写父类的excute函数便可
    def execute(self, args, params=None, context=None):
        # 获取到相应的处理函数
        api_name = args[0]
        result = dict()
        try:
            res = openApiFactory.execute_open_api(api_name, params)
            result["success"] = True
            result["result"] = res
        except Exception as error:
            result["success"] = False
            result["error_msg"] = str(error)
            logging.error(
                f"execute_open_api:{api_name}:params:{params}:error:{error}"
            )
            logging.exception(traceback.format_exc())
            
        
        return result
